﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Fjaspx.Common;

namespace Fjaspx.UI.Web
{
    public partial class _Login : UserPage
    {
        private string _username = null;//用户名
        private string _password = null;//密码
        private string _k = null;//快捷登陆
        private string _valdates = null;//验证码
        private string _Url = null;//登陆后返回URL
        public string username
        {
            get
            {
                if (Request["username"] != null) { _username = Request["username"]; }
                return _username;
            }
            set
            {
                _username = value;
            }
        }
        public string password
        {
            get
            {
                if (Request["password"] != null) { _password = Request["password"]; }
                return _password;
            }
            set
            {
                _password = value;
            }
        }
        public string k
        {
            get
            {
                if (Request["k"] != null) { _k = Request["k"]; }
                return _k;
            }
            set
            {
                _k = value;
            }
        }
        public string valdates
        {
            get
            {
                if (Request["valdates"] != null) { _valdates = Request["valdates"]; }
                return _valdates;
            }
            set
            {
                _valdates = value;
            }
        }
        /// <summary>
        /// Gets or sets the URL.
        /// </summary>
        /// <value>The URL.</value>
        public string Url
        {
            get
            {
                if (Fjaspx.Common.Session.Get("HistoryUrl") != null) { _Url = ___WebConfig.web_host + Fjaspx.Common.Session.Get("HistoryUrl") + "?GUID=" + ___WebConfig.GUID; }
                else { _Url = ___WebConfig.web_host + "ucenter/default.aspx"; }
                return _Url;
            }
            set
            {
                _Url = value;
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        { login_Click(); }
        /// <summary>
        /// Handles the Click event of the Button_login control.
        /// </summary>
        void login_Click()
        {
            if (this.k == "0") { this.valdates = "0"; Fjaspx.Common.Session.Add("code","0"); } //快捷登陆
            if (this.username != null && this.password != null && this.valdates != null)
            {
                ___ROYcms_user_model = ___ROYcms_user_bll.GetModel(this.username.Trim());
                if (this.valdates.Trim().ToUpperInvariant() == Fjaspx.Common.Session.Get("code"))
                {

                    //子站验证成功
                    if (___ROYcms_user_bll.Exists(this.username.Trim(), this.password.Trim()))
                    {
                        //插入主站信息 未实现
                        Fjaspx.Common.Session.Add("GUID", ___ROYcms_user_model.GUID);
                        Fjaspx.Common.Session.Add("user_id", ___ROYcms_user_model.bh.ToString().Trim());
                        Fjaspx.Common.Session.Add("ugroup_id", ___ROYcms_user_model.UgroupID.ToString().Trim());
                        Fjaspx.Common.Session.Add("user", ___ROYcms_user_model.name.Trim());
                        MessageBox.ShowAndRedirect(this, "登录成功！将直接转到会员后台！  2秒钟自动跳转", this.Url);
                        Response.Write("登录成功！<a href='" + this.Url + "'>点击进入" + this.Url + "</a>");
                        Response.Write("<SCRIPT LANGUAGE='JavaScript'>redirTime = '2000';redirURL = '" + this.Url + "';function redirTimer() { self.setTimeout('self.location.href = redirURL;',redirTime); }</script>");
                    }
                    else
                    {
                        if (index_Login(username, password) != 0)
                        {
                            if (index_Login(this.username, this.password) == 2) { MessageBox.ShowAndRedirect(this, "登录成功！将直接转到会员后台！[本站会员]", this.Url); }
                            if (index_Login(this.username, this.password) == 3) { MessageBox.ShowAndRedirect(this, "站群通行证登录成功！将直接转到首页！[非本站会员]", this.Url); }
                        }
                        else { MessageBox.ShowAndRedirect(this, "登录失败！", "?oerr"); Response.Write("登录出错！"); }
                    }


                }
                else { MessageBox.Show(this, "验证码错误！"); Response.Write("验证码错误！"); }
            }
            else { Response.Write("登录出错！"); }

        }

        /// <summary>
        /// Index_s the login.
        /// </summary>
        /// <returns></returns>
        public int index_Login(string username, string password)
        {

            int WC_e = 0;

            //if (___WebConfig.UcenterWebserver != "index")
            //{
            //    Fjaspx.UI.Web.WC_ROYcms_user.ROYcms_user User = new Fjaspx.UI.Web.WC_ROYcms_user.ROYcms_user();
            //    Fjaspx.UI.Web.WC_ROYcms_user.ROYcms_user1 model = new Fjaspx.UI.Web.WC_ROYcms_user.ROYcms_user1();
            //    User.Url = ___WebConfig.UcenterWebserver + "administrator/WebService/ROYcms_user.asmx";
            //    try
            //    {
            //        model = User._GetModel(username.Trim());
            //    }
            //    catch
            //    {
            //        WC_e = 0;
            //    }
            //    //获取设置主站 webserver地址

            //    if (User.Exists(username.Trim(), password.Trim()))
            //    {
            //        WC_e = 1;
            //    }


            //    //主站验证成功
            //    if (WC_e == 1 && model != null)
            //    {
            //        try
            //        {
            //            Fjaspx.Common.Session.Add("GUID", model.GUID);
            //            Fjaspx.Common.Session.Add("user_id", model.bh.ToString());
            //            Fjaspx.Common.Session.Add("user", model.name.Trim());
            //            //本站会员
            //            if (model.GUID.Trim() == ___WebConfig.GUID.Trim()) { WC_e = 2; }
            //            //非本站会员
            //            else { WC_e = 3; }
            //        }
            //        catch { WC_e = 0; }
            //    }
            //}
            return WC_e;
        }

    }
}
